﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.SalesActivity
{
    public partial class VerificationSummary : SOSecurityPage
    {
        StructureFacade fac = new StructureFacade();
        int userType;
        User user;
        int flag;
        int userId;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Session.Add("flag", 0);


                if (!IsPostBack)
                {
                    if (radioButtonDuePolicy.Checked == true)
                    {
                        txtDueDateFrom.Text = System.DateTime.Today.ToShortDateString();
                        txtDueDateTo.Text = System.DateTime.Today.ToShortDateString();

                        drpYear.Visible = false;
                        drpMonth.Visible = false;
                        Label2.Visible = false;
                        //vldMonth.Visible = false;
                        //vldYear.Visible = false;
                        ActiveFunction();
                    }


                    //gets the user type

                    ValidateUserCredentials(Request.Url.LocalPath);

                    User user = fac.GetUser(User.Identity.Name);
                    UserInformation info = fac.GetUserInformation(User.Identity.Name);
                    userType = user.UserType;
                    userId = user.NodeId;


                    if (userType == 1)
                    {
                        FillDropDown("Branch");
                    }
                    else if (userType == 2)
                    {
                        FillDropDown("BranchGrp");
                    }
                    else if (userType == 3)
                    {

                        UserForBranch();


                    }
                    else if (userType == 4)
                    {
                        UserForUnit();


                    }
                }
            }
            catch (Exception ex) { }

        }

        protected void radioButtonSC_CheckedChanged(object sender, EventArgs e)
        {
            btnSearch1.Visible = true;
            btnSearch.Visible = false;
            try
            {
                ActiveFunctionSC();
            }
            catch (Exception ex) { }
            
        }

        protected void radioButtonDuePolicy_CheckedChanged(object sender, EventArgs e)
        {
            btnSearch.Visible = true;
            btnSearch1.Visible = false;
            try
            {
                ActiveFunction();
            }
            catch (Exception ex) { }
            
        }


        public void FillDropDown(string type)
        {
            switch (type)
            {

                case "BranchGrp":

                    List<Branch> br1 = fac.GetBranchesForGroup(userId);

                    Branch branch1 = new Branch();
                    branch1.BranchId = -1;
                    branch1.BranchName = "Select...";
                    br1.Insert(0, branch1);
                    drpBranch.DataSource = br1;
                    drpBranch.DataTextField = "BranchName";
                    drpBranch.DataValueField = "BranchId";
                    drpBranch.DataBind();

                    break;



                case "Branch":
                   
                    

                        List<Branch> br = fac.GetBranches("BranchName ASC");
                        Branch branch = new Branch();
                        branch.BranchId = -1;
                        branch.BranchName = "Select...";
                        br.Insert(0, branch);
                        drpBranch.DataSource = br;
                        drpBranch.DataTextField = "BranchName";
                        drpBranch.DataValueField = "BranchId";
                        drpBranch.DataBind();
                   
                    

                    break;

                case "Unit":
                    if (drpBranch.SelectedValue != "-1")
                    {

                         if (drpSOcode.SelectedItem.Value != "Select...")
                    {
                        List<string> soCode = new List<string>();
                        soCode.Insert(0, "Select...");
                        drpSOcode.DataSource = soCode;
                        drpSOcode.DataBind();

                        //drpSOcode.Items.Clear();
                    }


                        List<SalesOneUnit> un = fac.GetUnitsInBranch(Convert.ToInt32(drpBranch.SelectedValue));
                        SalesOneUnit unSel = new SalesOneUnit();

                        if (un.Count != 0)
                        {
                            //lblResult.Visible = false;
                            btnSearch.Enabled = true;
                            drpUnit.Enabled = true;
                            drpSOcode.Enabled = true;
                            //Image1.Visible = true;
                            //Image2.Visible = true;
                            txtDueDateFrom.Enabled = true;
                            txtDueDateTo.Enabled = true;


                            unSel.UnitId = -1;
                            unSel.Name = "Select...";
                            un.Insert(0, unSel);

                            drpUnit.DataSource = un;
                            drpUnit.DataTextField = "Name";
                            drpUnit.DataValueField = "UnitId";
                            drpUnit.DataBind();
                        }
                        else
                        {
                            //lblResult.Visible = true;
                            //lblResult.Text = "No more Units for this branch";
                            //lblResult.ForeColor = System.Drawing.Color.Red;

                            MassageSummery("IIS504");
                            
                            drpUnit.Enabled = false;
                            drpSOcode.Enabled = false;
                            //Image1.Visible = false;
                            //Image2.Visible = false;
                            txtDueDateFrom.Enabled = false;
                            txtDueDateTo.Enabled = false;
                            btnSearch.Enabled = false;
                        }
                    }

                    break;

                case "UnitOrg":
                    List<SalesOneUnit> unt1 = fac.GetUnits("UnitId ASC");
                    SalesOneUnit sou = new SalesOneUnit();

                    sou.UnitId = -1;
                    sou.Name = "Select";

                    unt1.Insert(0, sou);
                    drpUnit.DataSource = unt1;
                    drpUnit.DataTextField = "Name";
                    drpUnit.DataValueField = "UnitId";

                    drpUnit.DataBind();


                    break;

                case "SOCode":
                    if (drpUnit.SelectedValue != "-1")
                    {
                        //catch the error msg using this place. use try catch. For this to be successed write exception handling for the linqUser---- class
                        List<string> socode = fac.GetSOsInUnit(Convert.ToInt32(drpUnit.SelectedValue));
                        if (socode.Count != 0 && radioButtonDuePolicy.Checked == true)
                        {


                            btnSearch.Enabled = true;
                            //lblResult.Visible = true;
                            txtDueDateFrom.Enabled = true;
                            txtDueDateTo.Enabled = true;
                            drpSOcode.Enabled = true;
                            Image1.Visible = true;
                            Image2.Visible = true;
                            string str = "Select...";
                            socode.Insert(0, str);
                            drpSOcode.DataSource = socode;
                            drpSOcode.DataBind();

                        }
                        else if (radioButtonDuePolicy.Checked == true)
                        {
                            //lblResult.Visible = true;
                            //lblResult.Text = "No more SOCodes to this user";
                            //lblResult.ForeColor = System.Drawing.Color.Red;

                            MassageSummery("IIS504");

                            drpSOcode.Enabled = false;
                            //Image1.Visible = false;
                            //Image2.Visible = false;
                            txtDueDateFrom.Enabled = false;
                            txtDueDateTo.Enabled = false;
                            btnSearch.Enabled = false;
                        }


                    }
                    break;

                case "Month":

                   

                    DateTimeFormatInfo format = new DateTimeFormatInfo();

                    drpMonth.Items.Clear();
                    drpMonth.DataSource = format.MonthNames;
                    //ListItem newItem = new ListItem("Select...");
                    //drpMonth.Items.Insert(0, newItem);
                    

                    for (int i = 1; i <= 12; i++)
                    {
                        ListItem item = new ListItem(format.MonthNames[i - 1], i.ToString());
                        
                        drpMonth.Items.Add(item);
                    }
                    break;

                case "Year":


                    drpYear.Items.Clear();
                    //ListItem firstItem = new ListItem("Select...");
                    //drpYear.Items.Insert(0, firstItem);


                    for (int i = 0; i < 10; i++)
                    {
                        drpYear.Items.Add((DateTime.Today.Year - i).ToString());

                    }
                    break;


                default:
                    break;
            }
        }

        protected void drpBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDropDown("Unit");
        }

        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {

            FillDropDown("SOCode");


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (drpSOcode.SelectedIndex > 0 && txtDueDateFrom.Text != "" && txtDueDateTo.Text != "")
                {

                    if (radioButtonDuePolicy.Checked == true)
                    {
                        string fromDate = txtDueDateFrom.Text;
                        string toDate = txtDueDateTo.Text;
                        string SOCode = drpSOcode.SelectedItem.ToString();


                        List<Policy> pol = fac.GetPoliciesForDate(Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate).AddDays(1), SOCode);

                        if (pol.Count != 0)
                        {

                            Response.Redirect("~/SalesActivity/VerificationSummary.aspx?DueFromDate=" + fromDate + "&DueToDate=" + toDate + "&SOCode=" + SOCode);
                        }
                        else
                        {
                            MassageSummery("IIS504");
                        }
                    }
                }
                else if (drpSOcode.SelectedIndex <= 0)
                {
                    MassageSummery("IIS505");
                }
            }
            catch (Exception ex) { }

        }

        //if radio policy is selected

        public void ActiveFunction()
        {
            User user = fac.GetUser(User.Identity.Name);
            userType = user.UserType;          


            Label2.Visible = false;
            drpMonth.Visible = false;
            drpYear.Visible = false;
            drpYear.Enabled = false;
            drpMonth.Enabled = false;
            lblSOCode.Visible = true;
            drpSOcode.Visible = true;
            lblDateFrom.Visible = true;
            lblDateTo.Visible = true;
            txtDueDateFrom.Visible = true;
            txtDueDateTo.Visible = true;

            //vldMonth.Visible = false;
            //vldYear.Visible = false;
            drpSOcode.ClearSelection();
            drpBranch.ClearSelection();
            drpUnit.ClearSelection();

            txtDueDateFrom.Text = System.DateTime.Today.ToShortDateString();
            txtDueDateTo.Text = System.DateTime.Today.ToShortDateString();

            //txtDueDateFrom.Text = "";
            //txtDueDateTo.Text = "";
            Image1.Visible = true;
            Image2.Visible = true;



            if (userType == 3)
            {
                UserForBranch();
            }
            else if (userType == 4)
            {
                UserForUnit();
            }


        }

        //if SC not submitted is selected

        public void ActiveFunctionSC()
        {
            
            User user = fac.GetUser(User.Identity.Name);
            userType = user.UserType;

            drpBranch.Enabled = true;
            drpUnit.Enabled = true;
            //lblResult.Visible = false;
            drpYear.Visible = true;
            drpMonth.Visible = true;
            Label2.Visible = true;
            //lblBranch.Visible = false;
            //lblCode.Visible = false;            
            drpMonth.Enabled = true;
            drpYear.Enabled = true;
            //vldMonth.Enabled = true;
            //vldYear.Enabled = true;
            drpSOcode.Visible = false;
            txtDueDateFrom.Visible = false;
            txtDueDateTo.Visible = false;
            lblDateTo.Visible = false;
            lblDateFrom.Visible = false;
            lblSOCode.Visible = false;
            //vldMonth.Visible = true;
            //vldYear.Visible = true;
            vldDateFrom.Visible = false;
            vldDateTo.Visible = false;
            drpSOcode.ClearSelection();
            drpBranch.ClearSelection();
            drpUnit.ClearSelection();
            txtDueDateFrom.Text = "";
            txtDueDateTo.Text = "";
            Image1.Visible = false;
            Image2.Visible = false;

            if (userType == 3)
            {
                UserForBranch();
            }
            else if (userType == 4)
            {
                UserForUnit();
            }



            
                FillDropDown("Month");
                drpMonth.SelectedValue = DateTime.Now.Month.ToString();
                FillDropDown("Year");
                drpYear.SelectedValue = DateTime.Now.Year.ToString();
        
             
         
                
            
            
        }

        //if logged user is a branch user
        public void UserForBranch()
        {
            User user = fac.GetUser(User.Identity.Name);
            UserInformation info = fac.GetUserInformation(User.Identity.Name);
            userType = user.UserType;

            FillDropDown("Branch");
            drpBranch.SelectedValue = info.NodeId.ToString();
            drpBranch.Enabled = false;
            FillDropDown("Unit");
        }

        //if logged user is a unit user
        public void UserForUnit()
        {

            User user = fac.GetUser(User.Identity.Name);
            UserInformation info = fac.GetUserInformation(User.Identity.Name);
            userType = user.UserType;

            FillDropDown("Branch");
            FillDropDown("UnitOrg");

            drpBranch.SelectedValue = info.BranchId.ToString();
            drpBranch.Enabled = false;
            drpUnit.SelectedValue = info.NodeId.ToString();
            drpUnit.Enabled = false;

            FillDropDown("SOCode");

        }

        protected void btnSearch1_Click(object sender, EventArgs e)
        {
            try
            {

                if (drpUnit.SelectedIndex > 0 && drpMonth.SelectedIndex > 0)
                {


                    if (radioButtonSC.Checked == true)
                    {
                        //vldMonth.Enabled = true;
                        //vldYear.Enabled = true;
                        string unitId = drpUnit.SelectedValue.ToString();
                        string unitName = drpUnit.SelectedItem.ToString();
                        string month = drpMonth.SelectedValue.ToString();
                        string year = drpYear.SelectedValue.ToString();
                        string branch = drpBranch.SelectedItem.ToString();
                        string monthName = drpMonth.SelectedItem.ToString();




                        List<User> app = fac.GetUsersForSOcode(Convert.ToInt32(unitId), Convert.ToInt32(month), Convert.ToInt32(year));
                        //List<User> res = fac.GetUserForSOCOde(soCode);

                        if (app.Count != 0)
                        {
                            Response.Redirect("~/SalesActivity/VerificationSummary.aspx?unitId=" + unitId + "&Month=" + month + "&Year=" + year + "&Branch=" + branch + "&MonthName=" + monthName + "&UnitName=" + unitName);
                        }
                        ////else if(res.Count != 0)
                        ////{
                        ////    Response.Redirect("~/SalesActivity/VerificationSummary.aspx?unitId=" + unitId + "&Month=" + month + "&Year=" + year + "&Branch=" + branch + "&MonthName=" + monthName + "&UnitName=" + unitName);

                        //}
                        else
                        {
                            MassageSummery("IIS504");
                        }

                    }
                }
                else if (drpUnit.SelectedIndex <= 0 || drpMonth.SelectedIndex <= 0 || drpYear.SelectedIndex <= 0)
                {
                    MassageSummery("IIS505");
                }

            }
            catch (Exception ex) { }



            }
        
        }



    }




